package controller

import (
	"github.com/kataras/iris/v12"
	"github.com/kataras/iris/v12/mvc"
	"gowork/meeting/model"
	"strconv"
)

type MeetingController struct {
	Ctx  iris.Context
	Meeting *model.Meeting
}

func (m *MeetingController)PostReserve() mvc.Result {

	m.Meeting = new(model.Meeting)

	titel := m.Ctx.FormValue("titel")
	conferenceRoomIdStr := m.Ctx.FormValue("conferenceRoomId")
	startTime := m.Ctx.FormValue("startTime")
	endTime := m.Ctx.FormValue("endTime")

	conferenceRoomId, err := strconv.ParseInt(conferenceRoomIdStr, 10, 64)
	if err != nil{
		return mvc.Response{
			Object : map[string]interface{}{"message": err.Error(), "data": nil, "code":500,},
		}
	}
	res, err := m.Meeting.Reserve(titel, conferenceRoomId, startTime, endTime)
	if err != nil{
		return mvc.Response{
			Object : map[string]interface{}{"message": err.Error(), "data": nil, "code":500,},
		}
	}

	return mvc.Response{
		Object : map[string]interface{}{"message": res["message"], "data": nil, "code":res["code"],},
	}
}

func (m *MeetingController)PostReservebytype() mvc.Result {

	m.Meeting = new(model.Meeting)

	titel := m.Ctx.FormValue("titel")
	conferenceRoomTypeIdStr := m.Ctx.FormValue("conferenceRoomTypeId")
	startTime := m.Ctx.FormValue("startTime")
	endTime := m.Ctx.FormValue("endTime")

	conferenceRoomTypeId, err := strconv.ParseInt(conferenceRoomTypeIdStr, 10, 64)
	if err != nil{
		return mvc.Response{
			Object : map[string]interface{}{"message": err.Error(), "data": nil, "code":500,},
		}
	}
	res, err := m.Meeting.ReserveByType(titel, conferenceRoomTypeId, startTime, endTime)
	if err != nil{
		return mvc.Response{
			Object : map[string]interface{}{"message": err.Error(), "data": nil, "code":500,},
		}
	}

	return mvc.Response{
		Object : map[string]interface{}{"message": res["message"], "data": nil, "code":res["code"],},
	}
}